/*
 * iShopPlace Project.
 */
package ishopplace.view.bean;

import org.springframework.beans.support.PagedListHolder;

/**
 * Abstract base class for backing beans, 
 * which need to manage pagination.
 * 
 * @author <a href="mailto:derek@derekshen.com">Derek Y. Shen</a>
 */
public abstract class BasePaginationBean extends BaseBean {
	//default paged object per page
	private static int DEFAULT_PAGE_SIZE = 4;
	private static final String PREVIOUS_PAGE = "previous";
	private static final String NEXT_PAGE = "next";
	protected static final String PAGE_PARAMETER_NAME = "page";
	
	//page navigation parameter
	private String page;
	
	//number of paged objects per page
	private int pageSize;
	
	//the holder of the paged object list
	protected PagedListHolder pagedListHolder;
	
	public BasePaginationBean() {
		super();
		this.pageSize = DEFAULT_PAGE_SIZE;
	}
	
	public void setPage(String newPage) {
		this.page = newPage;
	}
	
	public String getPage() {
		return this.page;
	}
	
	public void setPageSize(int newPageSize) {
		this.pageSize = newPageSize;
	}
	
	public int getPageSize() {
		return this.pageSize;
	}
	
	public PagedListHolder getPagedListHolder() {
		return this.pagedListHolder;
	}
	
	public void setPagedListHolder(PagedListHolder newPagedListHolder) {
		this.pagedListHolder = newPagedListHolder;
		this.pagedListHolder.setPageSize(this.pageSize);
	}
	
	/**
	 * Handle the pagination logic.
	 */
	protected void nagivate() {
  		if (NEXT_PAGE.equals(this.page)) {
			this.pagedListHolder.nextPage();
  		}
		else if ("previous".equals(this.page)) {
			this.pagedListHolder.previousPage();
  		}
	}
}
